package com.shoutao.website.controller.datacontroller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shoutao.website.contact.LoginRequest;
import com.shoutao.website.contact.LoginResponse;
import com.shoutao.website.contact.RegisterRequest;
import com.shoutao.website.contact.RegisterResponse;
import com.shoutao.website.entity.Userinfo;
import com.shoutao.website.mapper.UserinfoMapper;
import com.shoutao.website.utils.ObjectUtils;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.sql.SQLException;
import java.util.List;

/**
* @Description:
* @Param:
* @return:
* @Author: cqin
* @Date: 2019/11/5
*/
@RestController
@RequestMapping(value = "/api/user")
public class UserController {


    @Autowired
    private UserinfoMapper userinfoMapper;


    @ApiOperation(value = "登录接口", notes = "登录接口")
    @RequestMapping(value = "/login.do", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "request", value = "请求", dataType = "LoginRequest", required = true)
    })
    public LoginResponse login(HttpServletRequest httpServletRequest, LoginRequest request) throws SQLException {
        LoginResponse response = new LoginResponse();

        Userinfo sample = new Userinfo();
        sample.setUsername(request.getUsername());
        sample.setPassword(request.getPassword());
        List<Userinfo> userinfoList = userinfoMapper.selectList(new QueryWrapper<>(sample));

        if(userinfoList == null){
            response.setResultCode("Fail");
            response.setResultMessage("账户或者密码错误");
            return response;
        }


        HttpSession httpSession = httpServletRequest.getSession();
        httpSession.setAttribute("user", request.getUsername());

        // 设置一下cookie
        Cookie cookie = new Cookie("username", request.getUsername());
        cookie.setPath("/");
        cookie.setMaxAge(36000);

        HttpServletResponse httpServletResponse = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
        httpServletResponse.addCookie(cookie);

        response.setResult(true);
        response.setResultCode("SUCCESS");
        response.setResultMessage("登录成功");

        return response;

    }


    @ApiOperation(value = "注册接口", notes = "注册接口")
    @RequestMapping(value = "/register.do", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "request", value = "请求", dataType = "RegisterRequest", required = true)
    })
    public RegisterResponse register(HttpServletRequest httpServletRequest, RegisterRequest request) throws SQLException {

        RegisterResponse response = new RegisterResponse();

        Userinfo sample = new Userinfo();
        sample.setUsername(request.getPhoneNumber());
        List<Userinfo> userinfoList = userinfoMapper.selectList(new QueryWrapper<>(sample));

        if (!ObjectUtils.isNullOrEmpty(userinfoList)) {
            response.setResult(false);
            response.setResultCode("FAIL");
            response.setResultMessage("账号重复");
            return response;
        }


        Userinfo userinfo = new Userinfo();
        userinfo.setUsername(request.getPhoneNumber());
        userinfo.setPassword(request.getPassword());
        userinfo.setUsername(request.getPhoneNumber());
        userinfo.setRealname(request.getRealName());
        userinfo.setCompanyname(request.getCompanyName());
        userinfo.setPhomenumber(request.getPhoneNumber());
        userinfoMapper.insert(userinfo);
        response.setResult(true);
        response.setResultCode("SUCCESS");
        response.setResultMessage("注册成功");
        return response;

    }

}
